package com.emip.servlet.data;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.emip.bean.AlterLogBean;
import com.emip.bean.QuarterStoreBean;
import com.emip.bean.SiteItemBean;
import com.emip.pojo.Report;
import com.emip.pojo.Table;
import com.emip.service.QuarterStoreService;
import com.emip.util.TableFormat;
import com.emip.util.Util;



@Controller
@RequestMapping(value="/data/quarter")
public class QuarterDataAction extends DataManage {

	@Autowired
	QuarterStoreService quarterStoreService;
	
	@RequestMapping(value="/show")
	public ModelAndView index() {
		return new ModelAndView("/data/data", setDefaultParams(mmap));
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	@ResponseBody
	public Table<Map<String, Object>> getData(
			@RequestParam("startYear") int startYear, 
			@RequestParam("startQuarter") int startQuarter,
			@RequestParam("endYear") int endYear, 
			@RequestParam("endQuarter") int endQuarter,
			@RequestParam("siteId") String siteId,
			@RequestParam("page") int page,
			@RequestParam("rows") int size) 
			throws Exception {
		String[] itemIds = getItemIds(siteId);
		List<QuarterStoreBean> result = quarterStoreService.getByTimespan(startYear, startQuarter, endYear, endQuarter, itemIds, siteId, page, size);
		long records = quarterStoreService.getCount(startYear, startQuarter, endYear, endQuarter, itemIds, siteId);
		long totalPages = records / size + (records % size == 0 ? 0 : 1);
		Report report = formatReportBody(result);
		return new TableFormat<Map<String, Object>>().formTable(totalPages, page, records, report.getBodys());
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(
			@RequestParam("id") String time,
			@RequestParam("siteId") String siteId,
			HttpServletRequest request) throws ParseException {
		
		List<SiteItemBean> siteItems = siteItemService.getSiteItemBySiteID(siteId);
		String[] times = time != null ? time.split("年|季") : new String[]{};
		int year = times.length > 0 && Util.trim(times[0]) != "" 
					? Integer.parseInt(times[0]) : 0;
		int quarter = times.length > 1 && Util.trim(times[1]) != "" 
				? Integer.parseInt(times[1]) : 0;
		QuarterStoreBean store = new QuarterStoreBean();
		for (int i = 0; i < siteItems.size(); i++) {
			SiteItemBean siteItem = siteItems.get(i);
			String[] itemIds = new String[]{siteItem.getItem().getId()};
			String data = (String) request.getParameter(itemIds[0]);
			if (data == null || data.equals("")) continue;
			List<QuarterStoreBean> stores = quarterStoreService.getByTimespan(year, quarter, year, quarter, itemIds, siteId, 1, 100);
			AlterLogBean log = new AlterLogBean();
			log.setGenTime(new Date());
			log.setItem(siteItem.getItem());
			log.setSite(siteItem.getSite());
			log.setType("Quarter");
			if (stores == null || stores.size() == 0) {
				store.setYear(year + "");
				store.setQuater(quarter);
				store.setItem(siteItem.getItem());
				store.setSite(siteItem.getSite());
			} else {
				store = stores.get(0);
				log.setOldvalue(store.getValue());
			}
			store.setValue(data);
			setFlag(store);// 处理Flag
			quarterStoreService.merge(store);
			log.setNewvalue(data);
			log.setFlag("F");
			log.setDate(store.getTimeLabel());
			alterLogService.merge(log);
		}
		return store.getFlag();
	}
}
